//
//  CDDeleteAlert.swift
//  PDF
//
//  Created by Kellv on 2024/4/20.
//

import UIKit

class CDDeleteAlert: UIView {
    var actionHandler: (()->Void)?

    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: CDSCREEN_WIDTH, height: CDSCREEN_HEIGTH))
        
        let backGroundView = UIView(frame: frame)
        backGroundView.alpha = 0.6
        addSubview(backGroundView)
        
        // 毛玻璃效果
        let blurEffect = UIBlurEffect(style: .dark)
        let effectView = UIVisualEffectView(effect: blurEffect)
        effectView.frame = backGroundView.bounds
        backGroundView.addSubview(effectView)
        UIDevice.keyWindow().addSubview(self)
        UIDevice.keyWindow().bringSubviewToFront(self)
        
        let nib = UINib(nibName: "CDDeleteNib", bundle: nil)
        let newView = nib.instantiate(withOwner: self, options: nil).first as! CDDeleteNib

        newView.frame = CGRect(x: 16, y: (CDSCREEN_HEIGTH - 195)/2.0, width: frame.width - 32, height: 195)
        newView.layer.cornerRadius = 24
    
        self.addSubview(newView)
        newView.actionHandler = { [weak self] flag in
            guard let self = self else {
                return
            }
            
            if !flag {
                self.dismiss()
            } else {
                guard let actionHandler = self.actionHandler else {
                    return
                }
                self.dismiss()
                actionHandler()
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func show() {
        DispatchQueue.main.async {
            self.isHidden = false

            UIView.animate(withDuration: 0.25, animations: {
                self.minY = 0
            })
        }
    }
    
    @objc func dismiss() {
        self.isHidden = true
        self.minY = self.height
    }

}

class CDDeleteNib: UIView {
    
    @IBOutlet weak var subTitleLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var cancelBtn: UIButton!
    var actionHandler: ((Bool)->Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = .white

        titleLabel.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        titleLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 24)
        
        subTitleLabel.font = .regular(16)
        subTitleLabel.textColor = .black
        
        cancelBtn.setTitle("Cancel".localize(), for: .normal)
        cancelBtn.setTitleColor(.cancelColor, for: .normal)
        cancelBtn.titleLabel?.font = .helvBold(16)
        cancelBtn.layer.cornerRadius = 28
        cancelBtn.backgroundColor = .cancelColor
        
        deleteBtn.setTitle("Delete".localize(), for: .normal)
        deleteBtn.setTitleColor(.white, for: .normal)
        deleteBtn.titleLabel?.font = .helvBold(16)
        deleteBtn.layer.cornerRadius = 28

    }
    @IBAction func onCancelAction(_ sender: Any) {
           guard let actionHandler = actionHandler else {
               return
           }
        actionHandler(false)

    }
    
    @IBAction func onDeleteAction(_ sender: Any) {
        guard let actionHandler = actionHandler else {
            return
        }
        actionHandler(true)
    }
    
}
